package cn.iocoder.devlop.module.annual.report.websiteinfo.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.devlop.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.devlop.module.annual.report.baseinfo.dal.dataobject.BaseinfoDO;
import cn.iocoder.devlop.module.annual.report.baseinfo.service.BaseinfoService;
import cn.iocoder.devlop.module.annual.report.common.service.EnttypeEntclassifyService;
import cn.iocoder.devlop.module.annual.report.marpripinfo.dal.dataobject.MarpripinfoDO;
import cn.iocoder.devlop.module.annual.report.marpripinfo.service.MarpripinfoService;
import cn.iocoder.devlop.module.annual.report.utils.IdGenerateUtil;
import cn.iocoder.devlop.module.annual.report.utils.ObjectComparisonUtil;
import cn.iocoder.devlop.module.annual.report.utils.ReportYearHelper;
import cn.iocoder.devlop.module.annual.report.websiteinfo.controller.vo.WebsiteinfoBaseVO;
import cn.iocoder.devlop.module.annual.report.websiteinfo.controller.vo.WebsiteinfoCreateReqVO;
import cn.iocoder.devlop.module.annual.report.websiteinfo.controller.vo.WebsiteinfoOverwriteVO;
import cn.iocoder.devlop.module.annual.report.websiteinfo.controller.vo.WebsiteinfoUpdateReqVO;
import cn.iocoder.devlop.module.annual.report.websiteinfo.convert.WebsiteinfoConvert;
import cn.iocoder.devlop.module.annual.report.websiteinfo.dal.WebsiteinfoMapper;
import cn.iocoder.devlop.module.annual.report.websiteinfo.dal.dataobject.WebsiteinfoDO;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.devlop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.devlop.module.annual.report.enums.ErrorCodeConstants.WEBSITEINFO_NOT_EXISTS;

/**
 * 企业年报网站或网店信息 Service 实现类
 *
 * @author 孙浩
 */
@Service
@Validated
public class WebsiteinfoServiceImpl implements WebsiteinfoService {

    @Autowired
    private SqlSession sqlSession;

    @Resource
    private WebsiteinfoMapper websiteinfoMapper;

    @Resource
    private BaseinfoService baseinfoService;

    @Resource
    private MarpripinfoService marpripinfoService;

    @Resource
    private EnttypeEntclassifyService enttypeEntclassifyService;

    @Override
    public void createWebsiteinfo(WebsiteinfoBaseVO createReqVO) {
        // 插入
        BaseinfoDO baseinfoDO = baseinfoService.getBaseinfo(createReqVO.getRegno(), createReqVO.getAncheyear());
        if (ObjUtil.isNull(baseinfoDO)) {
            baseinfoService.initBaseinfo(createReqVO.getRegno(), createReqVO.getAncheyear());
        }
        WebsiteinfoDO websiteinfo = WebsiteinfoConvert.INSTANCE.convert(createReqVO);
        websiteinfo.setWebid(IdUtil.simpleUUID());
        websiteinfo.setAncheid(IdGenerateUtil.getAncheid(createReqVO.getRegno(), createReqVO.getAncheyear()));
        websiteinfoMapper.insert(websiteinfo);
    }

    @Override
    public void updateWebsiteinfo(WebsiteinfoUpdateReqVO updateReqVO) {
        // 设置年份
        ReportYearHelper.setYear(NumberUtil.parseInt(updateReqVO.getAncheyear()));
        if (null == updateReqVO.getWebid()) {
            createWebsiteinfo(updateReqVO);
        } else {
            validateWebsiteinfoExists(updateReqVO.getWebid(), updateReqVO.getAncheyear());
            WebsiteinfoDO updateObj = WebsiteinfoConvert.INSTANCE.convert(updateReqVO);
            websiteinfoMapper.updateById(updateObj);
        }
    }

    @Override
    public void deleteWebsiteinfo(String webid, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        // 校验存在
        validateWebsiteinfoExists(webid, ancheyear);
        // 删除
        websiteinfoMapper.deleteById(webid);
    }

    private void validateWebsiteinfoExists(String webid, String ancheyear) {
        WebsiteinfoDO websiteinfo = getWebsiteinfo(webid, ancheyear);
        if (null == websiteinfo) {
            throw exception(WEBSITEINFO_NOT_EXISTS);
        }
    }

    @Override
    public WebsiteinfoDO getWebsiteinfo(String webid, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        return websiteinfoMapper.selectById(webid);
    }

    @Override
    public List<WebsiteinfoDO> getWebsiteinfoList(String regno, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        return websiteinfoMapper.selectListByAncheid(ancheid);
    }


    @Override
    public void createWebsiteinfoByList(String regno, String ancheyear, List<WebsiteinfoDO> list) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        MarpripinfoDO marpripinfo = marpripinfoService.getMarpripinfo(regno);
        String entclassify = enttypeEntclassifyService.getEntclassify(marpripinfo.getEnttype());
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        BaseinfoDO baseinfo = baseinfoService.getBaseinfo(regno, ancheyear);
        if (null == baseinfo){
            baseinfoService.initBaseinfo(regno,ancheyear);
        }
        BaseinfoDO baseinfoDO = new BaseinfoDO();
        baseinfoDO.setWebsitsign("2");
        baseinfoDO.setAncheid(IdGenerateUtil.getAncheid(regno, ancheyear));
        if (CollUtil.isNotEmpty(list)) {
            for (WebsiteinfoDO web : list) {
                web.setWebid(IdUtil.simpleUUID());
                web.setAncheid(ancheid);
                web.setEntclassify(entclassify);
            }
            websiteinfoMapper.insertBatch(list);
            baseinfoDO.setWebsitsign("1");
        }
        baseinfoService.baseinfoUpdate(baseinfoDO);
    }

    @Override
    public void deleteWebsiteinfoAll(String regno, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        LambdaQueryWrapperX<WebsiteinfoDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eq(WebsiteinfoDO::getAncheid,ancheid);
        websiteinfoMapper.delete(wrapperX);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void overwriteWebsiteinfo(WebsiteinfoOverwriteVO updateReqVO) {
        BaseinfoDO baseinfo = baseinfoService.getBaseinfo(updateReqVO.getRegno(), updateReqVO.getAncheyear());
        if (ObjUtil.isNotEmpty(baseinfo)){
            if (StrUtil.equals(baseinfo.getState(),"02") ||StrUtil.equals(baseinfo.getState(),"03")) {
                List<WebsiteinfoDO> oldWebsiteinfoList = getWebsiteinfoList(updateReqVO.getRegno(), updateReqVO.getAncheyear());
                List<WebsiteinfoDO> newWebsiteinfoList = updateReqVO.getWebsiteinfoList();
                List<WebsiteinfoCreateReqVO> oldreqs = WebsiteinfoConvert.INSTANCE.convertList3(oldWebsiteinfoList);
                List<WebsiteinfoCreateReqVO> newreqs = WebsiteinfoConvert.INSTANCE.convertList3(newWebsiteinfoList);
                ObjectComparisonUtil.compareObjectList(oldreqs,newreqs,IdGenerateUtil.getAncheid(updateReqVO.getRegno(), updateReqVO.getAncheyear()),"股权变更");
            }
        }
        deleteWebsiteinfoAll(updateReqVO.getRegno(),updateReqVO.getAncheyear());
        createWebsiteinfoByList(updateReqVO.getRegno(),updateReqVO.getAncheyear(),updateReqVO.getWebsiteinfoList());
        baseinfoService.updateState(updateReqVO.getRegno(), updateReqVO.getAncheyear());
    }


}
